<?php

namespace aerovuelos\UbicacionBundle\Controller;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

use aerovuelos\UbicacionBundle\Entity\Provincias;
use aerovuelos\UbicacionBundle\Form\ProvinciasType;

/**
 * Provincias controller.
 *
 */
class ProvinciasController extends Controller
{
    /**
     * Lists all Provincias entities.
     *
     */
    public function indexAction()
    {
        $em = $this->getDoctrine()->getManager();

        $entities = $em->getRepository('UbicacionBundle:Provincias')->findAll();

        return $this->render('UbicacionBundle:Provincias:index.html.twig', array(
            'entities' => $entities,
        ));
    }

    /**
     * Creates a new Provincias entity.
     *
     */
    public function createAction(Request $request)
    {
        $entity  = new Provincias();
        $form = $this->createForm(new ProvinciasType(), $entity);
        $form->bind($request);

        if ($form->isValid()) {
            $em = $this->getDoctrine()->getManager();
            $em->persist($entity);
            $em->flush();

            return $this->redirect($this->generateUrl('provincias_show', array('id' => $entity->getId())));
        }

        return $this->render('UbicacionBundle:Provincias:new.html.twig', array(
            'entity' => $entity,
            'form'   => $form->createView(),
        ));
    }

    /**
     * Displays a form to create a new Provincias entity.
     *
     */
    public function newAction()
    {
        $entity = new Provincias();
        $form   = $this->createForm(new ProvinciasType(), $entity);

        return $this->render('UbicacionBundle:Provincias:new.html.twig', array(
            'entity' => $entity,
            'form'   => $form->createView(),
        ));
    }

    /**
     * Finds and displays a Provincias entity.
     *
     */
    public function showAction($id)
    {
        $em = $this->getDoctrine()->getManager();

        $entity = $em->getRepository('UbicacionBundle:Provincias')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Provincias entity.');
        }

        $deleteForm = $this->createDeleteForm($id);

        return $this->render('UbicacionBundle:Provincias:show.html.twig', array(
            'entity'      => $entity,
            'delete_form' => $deleteForm->createView(),        ));
    }

    /**
     * Displays a form to edit an existing Provincias entity.
     *
     */
    public function editAction($id)
    {
        $em = $this->getDoctrine()->getManager();

        $entity = $em->getRepository('UbicacionBundle:Provincias')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Provincias entity.');
        }

        $editForm = $this->createForm(new ProvinciasType(), $entity);
        $deleteForm = $this->createDeleteForm($id);

        return $this->render('UbicacionBundle:Provincias:edit.html.twig', array(
            'entity'      => $entity,
            'edit_form'   => $editForm->createView(),
            'delete_form' => $deleteForm->createView(),
        ));
    }

    /**
     * Edits an existing Provincias entity.
     *
     */
    public function updateAction(Request $request, $id)
    {
        $em = $this->getDoctrine()->getManager();

        $entity = $em->getRepository('UbicacionBundle:Provincias')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Provincias entity.');
        }

        $deleteForm = $this->createDeleteForm($id);
        $editForm = $this->createForm(new ProvinciasType(), $entity);
        $editForm->bind($request);

        if ($editForm->isValid()) {
            $em->persist($entity);
            $em->flush();

            return $this->redirect($this->generateUrl('provincias_show', array('id' => $id)));
        }

        return $this->render('UbicacionBundle:Provincias:edit.html.twig', array(
            'entity'      => $entity,
            'edit_form'   => $editForm->createView(),
            'delete_form' => $deleteForm->createView(),
        ));
    }

    /**
     * Deletes a Provincias entity.
     *
     */
   
    public function deleteAction(Request $request, $id) {
        $form = $this->createDeleteForm($id);
        $form->bind($request);

        if ($form->isValid()) {
            try {
                $em = $this->getDoctrine()->getManager();
                $entity = $em->getRepository('UbicacionBundle:Provincias')->find($id);

                if (!$entity) {
                    throw $this->createNotFoundException('Unable to find Provincias entity.');
                }

                $em->remove($entity);
                $em->flush();
            } catch (\Doctrine\DBAL\DBALException $e) {
                return $this->redirect($this->generateUrl('provincias_edit', array(
                                    'id' => $id,
                                    'error' => '1'
                )));
            }
        }

        return $this->redirect($this->generateUrl('provincias'));
    }

    /**
     * Creates a form to delete a Provincias entity by id.
     *
     * @param mixed $id The entity id
     *
     * @return Symfony\Component\Form\Form The form
     */
    private function createDeleteForm($id)
    {
        return $this->createFormBuilder(array('id' => $id))
            ->add('id', 'hidden')
            ->getForm()
        ;
    }
}
